﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Collections;

using HtmlAgilityPack;
using System.Data;
using WCrawlerLib;

namespace WCrawler.TestPages
{
    public partial class frmTestXPath : System.Web.UI.Page
    {
        private Forum _forum;
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                txtXPath.Text = @".//table[count(./thead)>=1 and count(./thead/tr/td)>1]/tbody/tr/td[2]/div//a";
                if (!string.IsNullOrEmpty(Request.QueryString["ForumId"]))
                {
                    if (Session[Request.QueryString["ForumId"]] != null)
                    {
                        _forum = (Forum)Session[Request.QueryString["ForumId"]];
                    }
                    else
                    {
                        _forum = new Forum();
                        _forum = _forum.GetForum(int.Parse(Request.QueryString["ForumId"]));
                        _forum.LoadForum();
                        Session[Request.QueryString["ForumId"]] = _forum;
                    }
                    txtForumUrl.Text = _forum.ForumUrl;
                }

            }
        }

        public DataTable CreateDataTable()
        {
            DataTable myTable;

            // Create a new DataTable.
            myTable = new DataTable("XPathResult");

            // Create DataColumn objects of data types.
            DataColumn colUrl = new DataColumn("Results");
            colUrl.DataType = System.Type.GetType("System.String");
            myTable.Columns.Add(colUrl);

            return myTable;
        }

        /// <summary>
        /// .//div[@class='foruminfo td']//a[text() != '']
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnFind_Click(object sender, EventArgs e)
        {
            List<string> result = new List<string>();
            lblException.Text = "";
            try
            {
                if (Session[Request.QueryString["ForumId"]] != null)
                {
                    _forum = (Forum)Session[Request.QueryString["ForumId"]];
                    HtmlNodeCollection nodeCollection = _forum.Browser.HtmlDoc.DocumentNode.SelectNodes(txtXPath.Text);
                    if (nodeCollection != null)
                    {
                        foreach (HtmlNode href in nodeCollection)
                            result.Add(href.OuterHtml);
                    }
                }
            }
            catch (Exception ex)
            {
                lblException.Text = ex.Message;
            }

            DataTable myTable = CreateDataTable();
            foreach (string value in result)
            {
                DataRow myNewRow = myTable.NewRow();
                myNewRow["Results"] = value;
                myTable.Rows.Add(myNewRow);
            }
            gridViewResult.DataSource = myTable;
            gridViewResult.DataBind();
        }

        protected void btnLoad_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["ForumId"]))
            {
                _forum = (Forum)Session[Request.QueryString["ForumId"]];
                if (txtForumUrl.Text != "")
                {
                    _forum.Browser.Get(txtForumUrl.Text);
                }
            }
            else
            {
                if (txtForumUrl.Text != "")
                {
                    _forum = new Forum();
                    //_forum.InitBrowserSession();
                    _forum.Browser.Get(txtForumUrl.Text);
                }
            }
            Session[txtForumUrl.Text] = _forum;
        }

        protected void btnViewContent_Click(object sender, EventArgs e)
        {
            if (!string.IsNullOrEmpty(Request.QueryString["ForumId"]))
            {
                _forum = (Forum)Session[Request.QueryString["ForumId"]];
                txtHtmlContent.Text = _forum.Browser.HtmlDoc.DocumentNode.OuterHtml.Replace(">", ">\n");
            }
            
        }
    }
}